package com.ruoyi.project.module.lyq;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.service.*;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 消费详细Controller
 * 
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/leq/details")
public class LeqYqgConsumptionDetailsController extends BaseController
{
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
    @Autowired
    private IYqgInformationProjectService yqgInformationProjectService;
    @Autowired
    private IYqgHandsService yqgHandsService;
//    @Autowired
//    private IYqgBaseinfoService yqgBaseinfoService;
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;

    /**
     * 查询消费详细列表
     */
//    @RequiresPermissions("project:details:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgConsumptionDetails yqgConsumptionDetails)
    {
        startPage();
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        return getDataTable(list);
    }

    /**
     * 导出消费详细列表
     */
//    @RequiresPermissions("project:details:export")
    @Log(title = "消费详细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgConsumptionDetails yqgConsumptionDetails)
    {
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        ExcelUtil<YqgConsumptionDetails> util = new ExcelUtil<YqgConsumptionDetails>(YqgConsumptionDetails.class);
        util.exportExcel(response, list, "消费详细数据");
    }

    /**
     * 获取消费详细详细信息
     */
//    @RequiresPermissions("project:details:query")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(yqgConsumptionDetailsService.selectYqgConsumptionDetailsByID(ID));
    }

    /**
     * 新增消费详细
     */
//    @RequiresPermissions("project:details:add")
    @Log(title = "消费详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        //获取手牌号信息
        YqgHands yqgHands = new YqgHands();
        yqgHands.setHandBrand(yqgConsumptionDetails.getHandBrand());
        List<YqgHands> listhands = yqgHandsService.selectYqgHandsList(yqgHands);
        for (YqgHands hands :listhands )
        {
//                yqgConsumptionDetails.setHandBrand(hands.getHandBrand());
            yqgConsumptionDetails.setHandCategory(hands.getHandCategory());
            yqgConsumptionDetails.setHandState(hands.getHandState());
        }
        // 获取吧台账单信息
        YqgBarBills yqgBarBills=new YqgBarBills();
        yqgBarBills.setHandBrand(yqgConsumptionDetails.getHandBrand());
        yqgBarBills.setHandState(yqgConsumptionDetails.getHandState());
        List<YqgBarBills> listbarbill = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
        for (YqgBarBills barbill :listbarbill )
        {
            yqgConsumptionDetails.setTogether(barbill.getTogether());
            yqgConsumptionDetails.setSailingsSigns(barbill.getSailings());
            yqgConsumptionDetails.setBarBillsId(barbill.getID());
            yqgConsumptionDetails.setRetentionStatus(barbill.getRetentionStatus());
            yqgConsumptionDetails.setMergeStatus(barbill.getMergeStatus());
            yqgConsumptionDetails.setOpeningTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",barbill.getOpeningTime()));

        }
//        // 获取当前登录用户信息
//        YqgBaseinfo yqgBaseinfo =new YqgBaseinfo();
//        String nickname=String.format("%04d",yqgConsumptionDetails.getUserId());
//        yqgBaseinfo.setNickName(nickname);
//        System.out.println(nickname);
//        List<YqgBaseinfo> listbaseinfo = yqgBaseinfoService.selectYqgBaseinfoList(yqgBaseinfo);
//        for (YqgBaseinfo baseinfos :listbaseinfo )
//        {
//            System.out.println("111111");
//            yqgConsumptionDetails.setDepartment(baseinfos.getDepartment());
//            yqgConsumptionDetails.setNickName(baseinfos.getNickName());
//            yqgConsumptionDetails.setUserName(baseinfos.getUserName());
//            yqgConsumptionDetails.setPost(baseinfos.getPost());
//        }


//
//        yqgInformationProject.setProjectCode(yqgConsumptionDetails.getProjectCode());
//        List<YqgInformationProject> Plist = yqgInformationProjectService.selectYqgInformationProjectList(yqgInformationProject);
//        yqgInformationProject=Plist.get(0);
        System.out.println("=======");
        System.out.println(yqgConsumptionDetails.getEntryName());
        YqgInformationProject yqgInformationProject=new YqgInformationProject();
        yqgInformationProject.setEntryName(yqgConsumptionDetails.getEntryName());
        List<YqgInformationProject> Plist = yqgInformationProjectService.selectYqgInformationProjectList(yqgInformationProject);
        for(YqgInformationProject InformationProject:Plist){
            yqgConsumptionDetails.setProjectCode(InformationProject.getProjectCode());
            yqgConsumptionDetails.setMnemonicCode(InformationProject.getMnemonicCode());
            yqgConsumptionDetails.setUnit(InformationProject.getUnit());
            yqgConsumptionDetails.setUnitPrice(InformationProject.getUnitPrice());
            yqgConsumptionDetails.setCommissionPrice(InformationProject.getCommissionPrice());
        }
            yqgConsumptionDetails.setRecordingTime(DateUtils.getNowDate());

//        yqgConsumptionDetails.setEntryName(yqgInformationProject.getEntryName());
//        yqgConsumptionDetails.setProjectCategory(yqgInformationProject.getProjectCategory());
//        yqgConsumptionDetails.setMnemonicCode(yqgInformationProject.getMnemonicCode());
//        yqgConsumptionDetails.setUnit(yqgInformationProject.getUnit());
//        yqgConsumptionDetails.setUnitPrice(yqgInformationProject.getUnitPrice());
//        yqgConsumptionDetails.setCommissionPrice(yqgInformationProject.getCommissionPrice());


//        for (YqgBarBills barBills:batailist){
//            yqgConsumptionDetails.setBarBillsId(barBills.getBarNumber());
//        }
        //注入并牌状态





//        //将手牌状态注入进消费详情表
//        YqgHands yqgHands = new YqgHands();
//        yqgHands.setHandBrand(yqgConsumptionDetails.getHandBrand());
//        List<YqgHands> listhand = yqgHandsService.selectYqgHandsList(yqgHands);
//        System.out.println("================");
//        System.out.println(listhand);
//        for(YqgHands hand:listhand )
//        {
//            yqgConsumptionDetails.setHandState(hand.getHandState());
//
//        }

        //已点商品的商品状态字段全变成0
        yqgConsumptionDetails.setConsumptionStatus("0");
        return toAjax(yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails));

    }

    /**
     * 修改消费详细
     */
//    @RequiresPermissions("project:details:edit")
    @Log(title = "消费详细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        return toAjax(yqgConsumptionDetailsService.updateYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 删除消费详细
     */
//    @RequiresPermissions("project:details:remove")
    @Log(title = "消费详细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByIDs(IDs));
    }
}
